package com.web.dao.bookrack;

import com.web.bean.bookrack.BookRack;
import com.web.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static java.lang.System.currentTimeMillis;

/**
 * @autor Chen
 * @create 2021--25-15:53
 * @desc
 */
public class BookRackDaoImpl implements BookRackDao {
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public List<BookRack> bookRacks() throws Exception {
        Connection conn = DBUtil.getInstance().getConnection();
        List<BookRack> bookRacks = new ArrayList<>();
        String sql = "select * from wn_bookrack where bstatus = 1 ORDER BY histime DESC";
        pst =conn.prepareStatement(sql);
        rs = pst.executeQuery();
        while (rs.next()){
            BookRack bookRack =createBookRack(rs);
            bookRacks.add(bookRack);
        }
        return bookRacks;
    }

    private BookRack createBookRack(ResultSet rs)  throws Exception{
        BookRack bookRack =new BookRack();
        bookRack.setUid(rs.getString(1));
        bookRack.setNid(rs.getString(2));
        bookRack.setNname(rs.getString(3));
        bookRack.setBstatus(rs.getInt(4));
        bookRack.setChapterId(rs.getString(5));
        bookRack.setHisTime(rs.getTimestamp(6));
        return bookRack;
    }

    @Override
    public List<BookRack> hisBook() throws Exception {
        Connection conn = DBUtil.getInstance().getConnection();
        List<BookRack> bookRacks = new ArrayList<>();
        String sql = "select * from wn_bookrack ORDER BY histime DESC";
        pst =conn.prepareStatement(sql);
        rs = pst.executeQuery();
        while (rs.next()){
            BookRack bookRack =createBookRack(rs);
            bookRacks.add(bookRack);
        }
        return bookRacks;
    }

    @Override
    public void addBookRack(BookRack bookRack) throws Exception {
        Connection conn = DBUtil.getInstance().getConnection();
        String sql = "insert into wn_bookrack values (?,?,?,?,?,?)";
        pst =conn.prepareStatement(sql);
        pst.setString(1,bookRack.getUid());
        pst.setString(2,bookRack.getNid());
        pst.setString(3,bookRack.getNname());
        pst.setInt(4,0);
        pst.setString(5,bookRack.getChapterId());
        pst.setTimestamp(6,new Timestamp(System.currentTimeMillis()));
        pst.executeUpdate();
        conn.close();
    }

    @Override
    //小说id
    public void deleteBookRack(String id) throws Exception {
        Connection conn = DBUtil.getInstance().getConnection();
        String sql = "delete from wn_bookrack where nid = ?";
        pst = conn.prepareStatement(sql);
        pst.setString(1,id);
        pst.executeUpdate();
        conn.close();
    }

    @Override
    public BookRack selectBookRackById(String id) throws Exception {
        BookRack bookRack =new BookRack();
        Connection conn = DBUtil.getInstance().getConnection();
        String sql = "select * from wn_bookrack where nid = ?";
        pst =conn.prepareStatement(sql);
        pst.setString(1,id);
        rs = pst.executeQuery();
        while (rs.next()){
            bookRack =createBookRack(rs);
        }
        conn.close();
        return bookRack;
    }

    @Override
    public void updateBookRack(BookRack bookRack) throws Exception {
        Connection conn  = DBUtil.getInstance().getConnection();
        String sql = "update wn_bookrack set bstatus = ? ,histime =? where nid = ?";
        pst = conn.prepareStatement(sql);
        pst.setInt(1,bookRack.getBstatus());
        pst.setTimestamp(2,new Timestamp(System.currentTimeMillis()));
        pst.setString(3,bookRack.getNid());
        pst.executeUpdate();
        conn.close();

    }
}
